package com.chen.controller;


import com.chen.pojo.entity.ContactGroup;
import com.chen.pojo.vo.ContactGroupVo;
import com.chen.results.Result;
import com.chen.service.ContactGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 陈子安
 * @since 2023-11-30
 */
@RestController
@RequestMapping("/contactGroup")
@Slf4j
@Api(tags = "联系人组接口")
public class ContactGroupController {
    @Autowired
    private ContactGroupService contactGroupService;

    @GetMapping
    @ApiOperation("获取用户的所有组信息")
    public Result<List<ContactGroupVo>> getContactGroupAll() {
        List<ContactGroupVo> contactGroupAll = contactGroupService.getContactGroupAll();
        return Result.success(contactGroupAll);
    }

    @GetMapping("{groupId}")
    @ApiOperation("根据组id查询组信息")
    public Result<ContactGroupVo> getContactGroupByGroupId(@PathVariable("groupId") Integer groupId) {
        log.info("要的获取组信息的id为:{}", groupId);
        return Result.success(contactGroupService.getContactGroupByGroupId(groupId));
    }

    @PostMapping
    @ApiOperation("添加组")
    public Result<ContactGroupVo> addContactGroup(@RequestBody ContactGroup contactGroup) {

     log.info("要添加的组的信息为：{}", contactGroup);
        ContactGroupVo newContactGroup = contactGroupService.addContactGroup(contactGroup);
        return Result.success(newContactGroup);
    }


    @DeleteMapping("{id}")
    @ApiOperation("根据组id删除用户组")
    public Result deleteContactGroupByGroupId(@PathVariable("id") Integer id) {
        log.info("要删除的组id为:{}", id);
        contactGroupService.deleteById(id);
        return Result.success();
    }


    @PutMapping("{id}")
    @ApiOperation("更新组信息")
    public Result<ContactGroupVo> updateGroupByGroupId(@PathVariable("id") Integer groupId, @RequestBody ContactGroup contactGroup) {
        contactGroup.setId(groupId);
        log.info("要更新的组信息,{}", contactGroup);
        ContactGroupVo contactGroupVo = contactGroupService.updateGroupByGroupId(contactGroup);
        return Result.success(contactGroupVo);
    }


}
